//
//  AppDelegate.m
//  Shelves
//
//  Created by 于洪东 on 2017/10/15.
//  Copyright © 2017年 上海LOL信息科技有限公司. All rights reserved.
//

#import "AppDelegate.h"
#import "AppDelegatePresenter.h"
#import "BaseTabBarController.h"
#import <WeexSDK.h>
#import "WeexImageDownloader.h"
#import "WeexEventModule.h"
#import "WeexButton.h"

@interface AppDelegate ()

@end


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
//    [self p_setAVOSCloud:launchOptions];
//    [self p_checkNetworkState];
//    [self p_setURLRequest];
    
    [self p_initWeex];
    [UIView wx_checkUIThread];
    [self p_setRootViewController];


    return YES;
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark --- 设置网络数据库
-(void)p_setAVOSCloud:(NSDictionary *)launchOptions{
    
    [AVOSCloud setApplicationId:@"DeXEgOm6FmLLVlX6EGV4NM0c-gzGzoHsz" clientKey:@"QmdWKs0gtr0IptjgR7H58sp4"];
    [AVAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
    [AVOSCloud setAllLogsEnabled:NO];
}

#pragma mark ---- 检查网络状况
-(void)p_checkNetworkState{
    [GLobalRealReachability startNotifier];
}

#pragma mark - 设置根视图,引导页
-(void)p_setRootViewController{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    
//    YYCache *yyCache=[YYCache cacheWithName:TYCacheName];
//    AppStartModel *model = (AppStartModel *)[yyCache objectForKey:TYCacheCheckKey];
//
//    NSString *appcheckString = [SRTool readValueForKey:isinCheck];

//    if ([model.statuscode isEqualToString:@"1"] || [appcheckString isEqualToString:@"1"]) {
//        // 在审核中
//        self.window.rootViewController = [[BaseWebViewController alloc]init];
//    }else{
//        BaseTabBarController *tabbar = [[BaseTabBarController alloc]init];
//        self.window.rootViewController = tabbar;
//    }
    
    BaseTabBarController *tabbar = [[BaseTabBarController alloc]init];
    self.window.rootViewController = tabbar;

}

-(void)p_setURLRequest{
    __block BOOL isfinished = NO;
    
    dispatch_group_t group = dispatch_group_create();
    
    AppDelegatePresenter *presenter = [[AppDelegatePresenter alloc]init];
    
    dispatch_group_enter(group);
    [presenter requestWebURL:^(BOOL succeeded, NSString *error) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    [presenter requestAppStartBlock:^(BOOL succeeded, NSString *error) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        isfinished = YES;
    });
    
    while (!isfinished) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
}

#pragma mark --- 初始化weex
-(void)p_initWeex{
    //1. 项目配置
    //1.1 设置组织
    [WXAppConfiguration setAppGroup:@"AliApp"];
    //1.2 设置App的名称
    [WXAppConfiguration setAppName:@"Shelves"];
    //1.3 设置App的版本号
    [WXAppConfiguration setAppVersion:@"1.0.0"];
    
    //2. 初始化`WeexSDK`环境
    [WXSDKEngine initSDKEnvironment];
    
    //3. 注册自定义的组件  [如果有就注册如果没有就不注册]
    [WXSDKEngine registerComponent:@"weex-button" withClass:[WeexButton class]];
    
    //4. 注册模块 [如果有就注册如果没有就不注册]
    [WXSDKEngine registerModule:@"mymoduleTest" withClass:[WeexEventModule class]];
    
    //5. 注册协议的实现,可选
    [WXSDKEngine registerHandler:[WeexImageDownloader new] withProtocol:@protocol(WXImgLoaderProtocol)];
    
    //6. 设置日志的级别(默认的日志级别是Info)
    [WXLog setLogLevel:WXLogLevelDebug];

}


@end
